package com.yanqu.road.server.cmd.activity.chuhan;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tongbing.ChuHanModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CROSS_CHUHAN_GET_UNION_MEMBER_REQ, desc = "锁名单后：获取商会成员信息 请求")
public class ChuHanGetUnionMemberReqCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ChuHanProto.CrossChuHanGetMemberReqMsg reqMsg = ChuHanProto.CrossChuHanGetMemberReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        List<Long> userIdList = reqMsg.getUserIdList();
        int groupId = reqMsg.getGroupId();

        ChuHanProto.CrossChuHanGetMemberRespMsg.Builder builder = ChuHanProto.CrossChuHanGetMemberRespMsg.newBuilder();
        builder.setActivityId(activityId);
        for (Long userId : userIdList) {
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo != null) {
                ChuHanProto.ChuHanUnionMemberMsg.Builder tmp = ChuHanProto.ChuHanUnionMemberMsg.newBuilder();
                tmp.setUserId(userId);
                tmp.setEarnSpeed(userInfo.getEarnSpeed().toString());
                builder.addMember(tmp);
            }

            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
            if (onlinePlayer != null) {//在线的传下队伍数据,生成兵
                onlinePlayer.getModule(ChuHanModule.class).crossLogin();
            }
        }

        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_CHUHAN_GET_UNION_MEMBER_RESP, builder, 0, activityId, groupId);
    }
}
